package com.cyh.nio;

import java.nio.ByteBuffer;

/**
 * 只读缓冲区， 对源缓冲区只读，源缓冲区数据改变，读缓冲区数据也改变
 * */
public class BufferDemo04 {

    public static void main(String[] args) throws Exception {
        ByteBuffer buffer = ByteBuffer.allocate(10);
        for(int i = 0; i < buffer.capacity(); i++) {
            buffer.put((byte)i);
        }

        //创建只读缓冲区
        ByteBuffer readOnly = buffer.asReadOnlyBuffer();

        //改变子缓存区的内容
        for(int i = 0; i < buffer.capacity(); i++) {
            byte b = buffer.get(i);;
            b *= 10;
            buffer.put(i, b);
        }

        readOnly.position(0);
        readOnly.limit(buffer.capacity());

        while (readOnly.remaining() > 0) {
            System.out.println(readOnly.get());
        }
    }
}
